package com.thirdgroup.service.impl;

import com.mysql.cj.jdbc.JdbcConnection;
import com.thirdgroup.dao.*;
import com.thirdgroup.dao.impl.*;
import com.thirdgroup.pojo.entity.CustomerInfo;
import com.thirdgroup.pojo.vo.CustomerPhoneTotalVo;
import com.thirdgroup.pojo.vo.PhoneTotalVo;
import com.thirdgroup.service.SchedulerService;
import com.thirdgroup.utils.DruidLocalUtil;
import com.thirdgroup.utils.respond.R;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SchedulerServiceImpl extends DruidLocalUtil implements SchedulerService {
    public PhoneTotalDao phoneTotalDao=new PhoneTotalDaoImpl();
    public CustomerPhoneTotalDao customerPhoneTotalDao=new CustomerPhoneToalDaoImpl();
    public CusInfoDao cusInfoDao=new CusInfoDaoImpl();
    public NoticeDao noticeDao=new NoticeDaoImpl();
    @Override
    public R automatic() {
        Connection connection = null;
        try {
            // 获取项目中已有的数据库连接
            connection = DruidLocalUtil.getConnection();
            List<CustomerPhoneTotalVo> customerPhoneTotalVos = customerPhoneTotalDao.customerToal();
            // 创建当前日期对象
            Date date = new Date();
            // 创建 SimpleDateFormat 对象，指定日期时间格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用 SimpleDateFormat 格式化日期时间
            String formattedDate = formatter.format(date);
            // 创建 Calendar 对象
            Calendar calendar = Calendar.getInstance();
            // 获取当前日期
            Date currentDate = new Date();
            // 将当前日期设置到 Calendar 对象中
            calendar.setTime(currentDate);
            // 减去一个月
            calendar.add(Calendar.MONTH, -1);
            // 获取上一个月的日期
            Date lastMonthDate = calendar.getTime();

            // 创建 SimpleDateFormat 对象，指定日期格式
            SimpleDateFormat formatters = new SimpleDateFormat("yyyy年MM月");
            // 使用 SimpleDateFormat 格式化日期
            String formattedLastMonthDate = formatters.format(lastMonthDate);
            for (CustomerPhoneTotalVo customerPhoneTotalVo : customerPhoneTotalVos) {
                String customerID = customerPhoneTotalVo.getCustomerID();
                CustomerInfo detail = cusInfoDao.detail(customerID);
                if (detail.getCustomerbalance()>= customerPhoneTotalVo.getTotal()){
                    Integer integer = cusInfoDao.updateBalance(customerPhoneTotalVo.getCustomerID(), customerPhoneTotalVo.getTotal());
                    noticeDao.create(detail.getCustomername()+"在"+formattedLastMonthDate+"总消费"+customerPhoneTotalVo.getTotal()+"元，已缴费成功！！",1,formattedDate);
                }else{
                    noticeDao.create(detail.getCustomername()+"在"+formattedLastMonthDate+"总消费"+customerPhoneTotalVo.getTotal()+"元，余额"+detail.getCustomerbalance()+"元，余额不足，请及时充值！！",1,formattedDate);

                }

            }
            for (PhoneTotalVo phoneTotalVo : phoneTotalDao.phoneTotal()) {
                CustomerInfo customerInfo =cusInfoDao.detail(phoneTotalVo.getCustomerID());
                noticeDao.create(customerInfo.getCustomername()+"客户的"+phoneTotalVo.getUserPhone()+"电话卡在"+formattedLastMonthDate+"通信时长"+phoneTotalVo.getDuration()+"分钟，花费"+phoneTotalVo.getTotal()+"元钱",2,formattedDate);
            }
            // 提交事务
            connection.commit();
            return R.ok("所有客户在"+formattedLastMonthDate+"费用已经结算完毕！！");

        } catch (Exception e) {
            // 发生异常时回滚事务
            try {
                if (connection != null) {
                    connection.rollback();
                    return R.error(500,"客户结算发生异常，请及时充值或稍后再试！");
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
            return null;
        } finally {
            // 不要关闭现有的数据库连接，因为它可能被其他方法使用
        }

    }
}
